package com.wo.cloud.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wo.cloud.admin.entity.SysClientDetails;
import com.wo.cloud.admin.service.ISysClientDetailsService;
import com.wo.cloud.admin.util.SysFeignConstant;
import com.wo.cloud.core.entity.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liry
 * @version 1.0
 * @date Created on 2021/5/12 9:27
 * Description: 第三方应用管理
 */

@Api(tags = "客户端模块")
@RestController
@RequestMapping(SysFeignConstant.SYS_CLIENT_BASE_URL)
public class SysClientController {

    @Autowired
    private ISysClientDetailsService sysClientDetailsService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    // 新增第三方应用
//    @Log(value = "新增第三方应用")
    @ApiOperation(value = "新增第三方应用")
    @PostMapping("/save")
    public Response save(@RequestBody SysClientDetails sysClientDetails) {
        sysClientDetails.setClientSecret(passwordEncoder.encode(sysClientDetails.getClientSecret()));
        sysClientDetailsService.save(sysClientDetails);
        return Response.success("新增成功");
    }

    //    @Log(value = "删除第三方应用")
    @ApiOperation(value = "删除第三方应用")
    @DeleteMapping("/del/{id}")
    public Response del(@PathVariable("id") String id) {
        boolean result = sysClientDetailsService.removeById(id);
        if (result) {
            return Response.success("删除成功");
        }
        return Response.fail("删除失败，请重试");
    }

    //    @Log(value = "列表展示第三方应用")
    @ApiOperation(value = "列表展示")
    @GetMapping("/list")
    public Response list() {
        List<SysClientDetails> sysClientDetailsList = sysClientDetailsService.list();
        return Response.data(sysClientDetailsList);
    }

    //    @Log(value = "分页展示第三方应用")
    @ApiOperation(value = "分页展示")
    @GetMapping("/page")
    public Response page(Page page) {
        IPage<SysClientDetails> sysClientDetailsIPage = sysClientDetailsService.selectByPage(page);
        return Response.data(sysClientDetailsIPage);
    }
}
